package com.zxbg.service.impl.systemtoolsServiceImpl;
import com.zxbg.dao.EmployeeDao;
import com.zxbg.dao.SystemtoolsLeftMenuDao;
import com.zxbg.dao.TenantDao;
import com.zxbg.domain.SystemtoolsLeftMenu;
import com.zxbg.domain.Tenant;
import com.zxbg.service.systemtoolsService.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private SystemtoolsLeftMenuDao systemtoolsLeftMenuDao;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private TenantDao tenantDao;

    @Override
    public List<SystemtoolsLeftMenu> getLeftMenu(String parent_code, Integer tenant_id, Set menu, boolean ifSysadmin) {
        List<SystemtoolsLeftMenu> resMenu = new ArrayList<SystemtoolsLeftMenu>();
        List<SystemtoolsLeftMenu> topMenu;
        topMenu = systemtoolsLeftMenuDao.getSystemtoolsLeftTargetMenu(parent_code, tenant_id);
        for (int i = 0; i < topMenu.size(); i++) {
            if(!ifSysadmin && !topMenu.get(i).getMenu_code().equals("main") && !menu.contains(topMenu.get(i).getMenu_code())) {
                boolean ifContinue = true;
                String childrenCodeString = topMenu.get(i).getChildren_code();
                if(childrenCodeString != null && !childrenCodeString.equals("")) {
                    List<String> childrenCodeList = Arrays.asList(childrenCodeString.split(","));
                    for (String childrenCode : childrenCodeList) {
                        if(menu.contains(childrenCode)) {
                            ifContinue = false;
                        }
                    }
                }
                if(ifContinue) {
                    continue;
                }
            }
            SystemtoolsLeftMenu tempMenu = topMenu.get(i);
            if(tempMenu.getChildren_code() != null) {
                tempMenu.setChildren(getLeftMenu(tempMenu.getMenu_code(), tenant_id, menu, ifSysadmin));
            }
            resMenu.add(tempMenu);
        }
        return resMenu;
    }

    @Override
    public int getCount(Integer tenant_id) {
        return employeeDao.getCount(tenant_id);
    }

    @Override
    public Tenant getBase(Integer tenant_id) {
        return tenantDao.getTenantById(tenant_id);
    }

    @Override
    public List<String> getAllMenuCode(Integer tenant_id) {
            return systemtoolsLeftMenuDao.getAllMenuCode(tenant_id);
    }
}
